package com.ziyue.socket.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ziyue.socket.bean.Member;
import com.ziyue.socket.bean.Room;
import com.ziyue.socket.bean.activity.Activity;
import com.ziyue.socket.bean.answer.AnswerRequest;
import com.ziyue.socket.bean.answer.AnswerRightResponse;
import com.ziyue.socket.bean.answer.RecordKey;
import com.ziyue.socket.bean.question.Answer;
import com.ziyue.socket.enums.OptionEnum;
import com.ziyue.socket.service.OptionAbstract;
import io.netty.channel.Channel;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2019/4/18.
 */
public class AnswerSend extends OptionAbstract {

    private final OptionEnum OPTION = OptionEnum.ANSWER_SEND;

    @Override
    public Object send(Channel channel, String requestBody) throws Exception {
        AnswerRequest request = JSONObject.parseObject(requestBody, AnswerRequest.class);
        Answer answer = new Answer();
        answer.setActivityId(request.getActivityId());
        answer.setQuestionId(request.getQuestionId());
        answer.setRightAnswer(request.getRightAnswer());
        answer.setAnswer(request.getAnswer());
        answer.setCurrentDate(request.getCurrentDate());

        List<String> answers = new ArrayList<>(request.getAnswer());
        List<String> rightAnswers = request.getRightAnswer();
        answer.setIfRight(false);
        //计算学生答案是否正确
        if (answers != null && answers.size() == rightAnswers.size()) {
            answers.removeAll(rightAnswers);
            if (answers.size() == 0) {
                answer.setIfRight(true);
            }
        }

        Member member = roomManage.getMembers().get(channel.id());
        Room room = roomManage.getRooms().get(member.getRoomId());
        answer.setAccount(member.getAccount());
        room.addAnswer(member.getAccount(), answer);
        sendAnswerToTeacher(channel,room);
        return null;
    }

    protected Map<String, Object> sendAnswerToTeacher(Channel channel,Room room) throws Exception {
        Map<String, Object> res = new HashMap<>(2);
        Map<String, Map<String, List<String>>> left = getLeft(room);
        List<AnswerRightResponse> right = getRight(room);
        res.put("left",left);
        res.put("right",right);
        List<Member> targetGroups = roomManage.targetGroups(channel);
        this.sendMsg(channels,targetGroups,"学生作答信息",res, OPTION);
        return res;
    }



    @Override
    public Object get(Channel channel, String requestBody) {
        return null;
    }


    public static void main(String[] args){
        /*float price=1.2f;
        DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        String p= decimalFormat.format(price);
        System.out.println(p);*/

        double   f   =   100 / 3;
        BigDecimal   b   =   new   BigDecimal(f);
        double   f1   =   b.setScale(3,   BigDecimal.ROUND_HALF_UP).doubleValue();

        System.out.println(100 / 3);
    }
}
